package org.jpro.common.jpa;

import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.domain.Sort.Direction.DESC;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

/**
 * <P>功能描述 : </P>
 * <P>Method Name : Sorts </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017-03-14 15:42:37</P>
 * @since 1.0
 */
public class Sorts {

    /**
     * <P>功能描述 : </P>
     * <P>Method Name : builder </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-14 15:42:37</P>
     * @return the builder
     * @since 1.0
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * <P>功能描述 : </P>
     * <P>Method Name : Builder </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-14 15:42:37</P>
     * @since x
     */
    public static final class Builder {
        /**
         * The Orders.
         */
        private List<Order> orders;

        /**
         * <P>功能描述 : </P>
         * <P>Method Name : Builder </P>
         * <P>Author : 王泽浩 </P>
         * <P>E-Mail : 1028625100@qq.com </P>
         * <P>Date : 2017-03-14 15:42:37</P>
         * @since 1.0
         */
        public Builder() {
            this.orders = new ArrayList<Order>();
        }

        /**
         * <P>功能描述 : </P>
         * <P>Method Name : asc </P>
         * <P>Author : 王泽浩 </P>
         * <P>E-Mail : 1028625100@qq.com </P>
         * <P>Date : 2017-03-14 15:42:37</P>
         * @param property the property
         * @return the builder
         * @since 1.0
         */
        public Builder asc(String property) {
            return asc(true, property);
        }

        /**
         * <P>功能描述 : </P>
         * <P>Method Name : desc </P>
         * <P>Author : 王泽浩 </P>
         * <P>E-Mail : 1028625100@qq.com </P>
         * <P>Date : 2017-03-14 15:42:37</P>
         * @param property the property
         * @return the builder
         * @since 1.0
         */
        public Builder desc(String property) {
            return desc(true, property);
        }

        /**
         * <P>功能描述 : </P>
         * <P>Method Name : asc </P>
         * <P>Author : 王泽浩 </P>
         * <P>E-Mail : 1028625100@qq.com </P>
         * <P>Date : 2017-03-14 15:42:38</P>
         * @param condition the condition
         * @param property the property
         * @return the builder
         * @since 1.0
         */
        public Builder asc(boolean condition, String property) {
            if (condition) {
                orders.add(new Order(ASC, property));
            }
            return this;
        }

        /**
         * <P>功能描述 : </P>
         * <P>Method Name : desc </P>
         * <P>Author : 王泽浩 </P>
         * <P>E-Mail : 1028625100@qq.com </P>
         * <P>Date : 2017-03-14 15:42:38</P>
         * @param condition the condition
         * @param property the property
         * @return the builder
         * @since 1.0
         */
        public Builder desc(boolean condition, String property) {
            if (condition) {
                orders.add(new Order(DESC, property));
            }
            return this;
        }

        /**
         * <P>功能描述 : </P>
         * <P>Method Name : build </P>
         * <P>Author : 王泽浩 </P>
         * <P>E-Mail : 1028625100@qq.com </P>
         * <P>Date : 2017-03-14 15:42:38</P>
         * @return the sort
         * @since 1.0
         */
        public Sort build() {
            return new Sort(orders);
        }
    }
}
